package com.xhwl.centiotdata.service.alarm.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.service.alarm.SubsystemPushService;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.common.dto.cent.alarm.AlarmIntegrationConfigDTO;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.pojo.cent.alarm.AlarmManagement;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service
@Slf4j
public class SubsystemPushServiceImpl implements SubsystemPushService {

    @Autowired
    private IDeviceCentService deviceCentService;

    public void sendAlarmPush(AlarmManagement alarmManagement, AlarmIntegrationConfigDTO alarmIntegrationConfigDTO) {
        try {
            Map<String, AlarmIntegrationConfigDTO.AlarmPushItem> alarmPushList = alarmIntegrationConfigDTO.getAlarmPushList();
            if (ObjectUtils.isNotEmpty(alarmPushList)) {
                for (Map.Entry<String, AlarmIntegrationConfigDTO.AlarmPushItem> i : alarmPushList.entrySet()) {
                    pushSubsystem(i.getKey(), i.getValue(), alarmManagement);
                }
            }
        } catch (Exception e) {
            log.error("告警推送失败: {}", e.getMessage());
        }
    }

    private void pushSubsystem(String key, AlarmIntegrationConfigDTO.AlarmPushItem alarmPushItem, AlarmManagement alarmManagement) {
        try {
            List<String> alarmCodeList = alarmPushItem.getAlarmCodeList();
            String url = alarmPushItem.getUrl();
            String alarmInventoryCode = alarmManagement.getAlarmInventoryCode();
            if (CollectionUtils.isNotEmpty(alarmCodeList) && !alarmCodeList.contains(alarmInventoryCode)) {
                log.info("不包含推送事件alarmInventoryCode ：{}, alarmCodeList: {}", alarmInventoryCode, JSON.toJSONString(alarmCodeList));
                return;
            }

            DevicePageQuery deviceQuery = new DevicePageQuery();
            deviceQuery.setId(alarmManagement.getDeviceId());
            List<DeviceDTO> deviceDTOList = deviceCentService.listDevice(null, deviceQuery);
            if (CollectionUtils.isEmpty(deviceDTOList)) {
                log.error("设备不存在DeviceId: {}", alarmManagement.getDeviceId());
                return;
            }
            String thirdDeviceId = deviceDTOList.get(0).getThirdDeviceId();
            if (Objects.equals(key, "mnXiaofang")) {
                log.info("蒙牛消防厂商消息开始推送===========");
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("aid", alarmInventoryCode);
                jsonObject.put("cid", alarmManagement.getDeviceId());
                jsonObject.put("ccode", thirdDeviceId);
                jsonObject.put("picUrl", alarmManagement.getImgUrl());
                if (ObjectUtils.isEmpty(alarmManagement.getAlarmTime())) {
                    alarmManagement.setAlarmTime(new Date());
                }
                jsonObject.put("time_stamp", alarmManagement.getAlarmTime().getTime() / 1000L);
                jsonObject.put("status", "1");
                HttpUtil.post(url, JSON.toJSONString(jsonObject));
                log.info("蒙牛消防厂商消息推送成功: {}", JSON.toJSONString(jsonObject));
            }
        } catch (Exception e) {
            log.error("蒙牛消防厂商消息告警推送失败: {}", e.getMessage());
        }
    }
}